//
//  YJBRegisterViewController.m
//  YunJiaBaoFirst
//
//  Created by 腾云小白龍 on 16/7/8.
//  Copyright © 2016年 腾云小白龍. All rights reserved.
//

#import "YJBRegisterViewController.h"
#import "YJBLoginPhoneNumView.h"
#import "YJBThirdLoginView.h"

@interface YJBRegisterViewController ()
@property (strong, nonatomic) UIScrollView * mainScrView;
@property (strong, nonatomic) UIButton * registerBtn;
@property (strong, nonatomic) YJBLoginPhoneNumView *phoneNumView;
@property (strong, nonatomic) NSTimer * timer;
@property (assign, nonatomic) int time;

@end

@implementation YJBRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initSubViews];
    [self setNavigationItemWithTitle:@"注册"];

}



-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}

#pragma mark -- 初始化定时器
-(NSTimer *)timer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeVerfyBtnAction) userInfo:nil repeats:YES];
    }
    return _timer;
}

#pragma mark -- 60s 后再次获取
-(void)changeVerfyBtnAction{
    self.time --;
    NSString * content = @"已发送 ";
    NSString * total = [content stringByAppendingString:[[NSNumber numberWithInt:self.time] stringValue]];
    [self.phoneNumView.verfyCodeButton setTitle:[total stringByAppendingString:@"s"] forState:UIControlStateNormal];

    if(self.time == 0) {
        [self.timer invalidate];
        _timer = nil;
        self.phoneNumView.verfyCodeButton.enabled = YES;
        [self.phoneNumView.verfyCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        
    }

   
}


#pragma mark-- 初始化Views
-(void)initSubViews{
    self.mainScrView = [[UIScrollView alloc]initWithFrame:KMainScreen.bounds];
    self.mainScrView.contentSize = CGSizeMake(KMainScreenSize.width, KMainScreenSize.height +1);
    self.mainScrView.scrollEnabled = YES;
    self.mainScrView.showsVerticalScrollIndicator   = NO;
    self.mainScrView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.mainScrView];
    
    CGFloat scaleHeight = KMainScreenSize.height/667;
    YJBLoginPhoneNumView * phoneNumView = [[YJBLoginPhoneNumView alloc]initWithFrame:CGRectMake(20,scaleHeight* 20,KMainScreenSize.width - 40,scaleHeight * 180)];
    self.phoneNumView = phoneNumView;
    phoneNumView.backgroundColor = [UIColor clearColor];
    phoneNumView.layer.cornerRadius = 5;
    phoneNumView.layer.masksToBounds = YES;
    [phoneNumView YJBRegisterView:phoneNumView];
    [self.mainScrView addSubview:phoneNumView];
    
    [phoneNumView.verfyCodeButton addTarget:self action:@selector(getVergyCode:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *procotolLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(phoneNumView.frame), KMainScreenSize.width - 40, 15)];
    procotolLabel.text = @"轻触下面的\"注册\"按钮,即表示你同意";
    procotolLabel.textColor = [UIColor grayColor];
    procotolLabel.font = [UIFont systemFontOfSize:10];
    procotolLabel.textAlignment = NSTextAlignmentCenter;
    [self.mainScrView addSubview:procotolLabel];
    
    UIButton *procotolBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(procotolLabel.frame)- 3, KMainScreenSize.width - 40, 20)];
    [procotolBtn setTitle:@"《运价宝软件许可及服务协议》" forState:UIControlStateNormal];
//    [procotolBtn setTitle:self.protocolTitle forState:UIControlStateNormal];

    [procotolBtn setTitleColor:KMainColor forState:UIControlStateNormal];
    [procotolBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    procotolBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [self.mainScrView addSubview:procotolBtn];
    [procotolBtn addTarget:self action:@selector(procotolDetailAction) forControlEvents:UIControlEventTouchUpInside];

    
    self.registerBtn = ({
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(procotolBtn.frame) + 5, KMainScreenSize.width - 40, 35)];
        [button setTitle:@"注册" forState:UIControlStateNormal];
        [button setTitleColor:KColorWithRGBA(192, 177, 178, 1)  forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        
        button.backgroundColor = KMainColor;
        [button addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [self.mainScrView addSubview:self.registerBtn];
    
    
    YJBThirdLoginView *thirdLoginView = [[YJBThirdLoginView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(self.registerBtn.frame) + KMainScreenSize.height * scaleHeight *150/667, KMainScreenSize.width - 30,scaleHeight * 120)];
    [thirdLoginView YJBThirdLoginView:thirdLoginView];
    [self.mainScrView addSubview:thirdLoginView];
    
}

#pragma mark -- 获取验证码
-(void)getVergyCode:(UIButton *)btn{
    if (!self.phoneNumView.phoneNumTextField.text.length) {
        [SVProgressHUD showImage:nil status:@"请输入手机号码"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    BOOL isPhoneNum = [HTUtils isMobileNumber:self.phoneNumView.phoneNumTextField.text];
    
    if (!isPhoneNum) {
        [SVProgressHUD showErrorWithStatus:@"您输入的手机号码不合法"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    btn.enabled = NO;
    self.time = 60;
    [self.timer fire];
}

#pragma mark -- 注册
-(void)registerBtnClick{
    if (!self.phoneNumView.phoneNumTextField.text.length) {
        [SVProgressHUD showImage:nil status:@"请输入手机号码"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    BOOL isPhoneNum = [HTUtils isMobileNumber:self.phoneNumView.phoneNumTextField.text];
    
    if (!isPhoneNum) {
        [SVProgressHUD showErrorWithStatus:@"您输入的手机号码不合法"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    
    if (!self.phoneNumView.passwordTextField.text.length) {
        [SVProgressHUD showImage:nil status:@"请输入密码"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
        
    }
    
    if (!self.phoneNumView.verfyCodeTextField.text.length) {
        [SVProgressHUD showImage:nil status:@"请输入验证码"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
}


//#pragma mark -- 返回登录
//-(void)backLogoInBtnClick{
//
////    [self dismissViewControllerAnimated:YES completion:nil];
//    [self.navigationController popViewControllerAnimated:YES];
//}

#pragma mark -- 服务协议
-(void)procotolDetailAction{
//    YJBProcotocolDetailController *vc = [[YJBProcotocolDetailController alloc]init];
//    vc.protocolTitle = self.protocolTitle;
//    vc.protocolContent = self.protocolContent;
//    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];

}



@end
